package com.nimble.smile.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.Spinner;
import android.widget.TextView;

import com.nimble.smile.R;
import com.nimble.smile.service.vo.AccountAuthDTO;
import com.nimble.smile.service.OperationCode;
import com.nimble.smile.service.vo.SignupDTO;
import com.nimble.smile.utils.SharedPreferenceHelper;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by simon on 17-5-4.
 */

public class RegisterActivity extends BaseActivity {

    @Bind(R.id.username)
    TextView userName;
    @Bind(R.id.phone)
    TextView phone;
    @Bind(R.id.password)
    TextView password;
    @Bind(R.id.password_confirm)
    TextView passwordConfirm;
    @Bind(R.id.department)
    Spinner department;
    @Bind(R.id.station)
    Spinner station;
    @Bind(R.id.office)
    Spinner office;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        ButterKnife.bind(this);
    }

    @OnClick(R.id.registerBtn)
    void register() {
        String name = userName.getText().toString();
        String phone = this.phone.getText().toString();
        String pwd = password.getText().toString();
        String pwdConfirm = passwordConfirm.getText().toString();
        String department = this.department.getSelectedItem().toString();
        String station = this.station.getSelectedItem().toString();
        String office = this.office.getSelectedItem().toString();

        if (!validEmpty(new String[][]{
                {name, "用户名不能为空"},
                {phone, "手机不能为空"},
                {pwd, "密码不能为空"},
                {department, "分公司不能为空"},
                {station, "收费站不能为空"},
                {office, "科室不能为空"}
        })) {
            return;
        }

        if (!pwd.equals(pwdConfirm)) {
            showToast("密码不一致");
            return;
        }

        SignupDTO signupDTO = new SignupDTO();
        signupDTO.setName(name);
        signupDTO.setMobile(phone);
        signupDTO.setPassword(pwd);
        signupDTO.setCompany(department);
        signupDTO.setStation(station);
        signupDTO.setOffice(office);


        showProgressDialog("注册中...");
        subscribe(getService().staffSignUp(signupDTO), response -> {
            hideProgressDialog();
            if (response.getOperationCode() == OperationCode.SUCCESS.getValue()) {
                showToast("注册成功");
                login(phone, pwd);
            } else {
                showToast(response.getMessage());
            }
        });

    }

    private void login(String username, String password) {
        showProgressDialog("登录中...");
        AccountAuthDTO account = new AccountAuthDTO();
        account.setMobile(username);
        account.setPassword(password);

        subscribe(getService().auth(account), response -> {
            hideProgressDialog();
            if (response.getOperationCode() == OperationCode.SUCCESS.getValue()) {
                SharedPreferenceHelper.setSharedPreferenceString("token", response.getBody());
                startActivity(new Intent(this, MainActivity.class));
                finish();
            } else {
                showToast(response.getMessage());
            }
        });
    }

    private boolean validEmpty(String[][] values) {
        for (String[] value : values) {
            if (TextUtils.isEmpty(value[0])) {
                showToast(value[1]);
                return false;
            }
        }
        return true;
    }


}
